import error from './error/index.js'
import behavior from './behavior/index.js'
import performance from './performance/index.js'
import { setConfig } from './config.js'
import { report } from './utils/report.js'
import { getCache, clearCache } from './utils/cache.js'
import { onHidden, onBeforeunload } from './utils/common.js'

const monitor = {
    // 初始化
    init(options = {}) {
        setConfig(options);
        error();
        behavior();
        performance();

        // 当页面进入后台或关闭前时，将所有的 cache 数据进行上报
        [onBeforeunload, onHidden].forEach(callBack  => {
            callBack(() => {
                const data = getCache()
                if (data.length) {
                    report(data, true)
                    clearCache()
                }
            })
        })
    },
    report,
}

export default monitor
